package com;

import java.util.concurrent.CountDownLatch;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
//import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import com.config.MyJdbcPro;
import com.config.MyServerYml;
import com.config.Receiver;
import com.upload.StorageProperties;
import com.upload.StorageService;

@SpringBootApplication
//定时任务开启
//@EnableScheduling
@EnableConfigurationProperties({MyServerYml.class,MyJdbcPro.class,StorageProperties.class})
//只限扫描mapper接口文件,否则会报错，导致mybatis不正常使用
@MapperScan("com.mapper")
public class Application extends SpringBootServletInitializer{
	//private static final Logger logger =  LoggerFactory.getLogger(Application.class);
	public static void main( String[] args )
    {
		SpringApplication.run(Application.class,args);
        //项目服务执行完毕后自动关闭
        //System.exit(0);
    }
	
	//初始化上传存储服务的bean，否则项目会由于没有扫到报错
	@Bean
	CommandLineRunner init(StorageService storageService) {
		return (args) -> {
			storageService.deleteAll();
			storageService.init();
		};
	}
	
	/*
	 * redis消息队列（消息传送过程中保存的容器）的实现3要素
	 * 1.一个连接工厂
	 * 2.Redis template模板
	 * 3.一个消息监听容器
	 * */
	
	//必要的redis消息队列连接工厂
	@Bean
    Receiver receiver(CountDownLatch latch) {
        return new Receiver(latch);
    }

	//必要的redis消息队列连接工厂
    @Bean
    CountDownLatch latch() {
        return new CountDownLatch(1);
    }

    //redis模板
    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }
    
    //注入消息监听器容器
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("msg"));
        return container;
    }

    //注入消息监听器容器
    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }
    
    //通过RestTemplate来实现消费服务
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

}

